﻿package net.area80.file {
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	
	/**
	* Dispatched
	* @eventType flash.events.ErrorEvent.ERROR
	*/	 
	[Event(name = "error", type = "flash.events.ErrorEvent")] 
	/**
	* Dispatched while loading an image
	* @eventType flash.events.ProgressEvent.PROGRESS
	*/	 
	[Event(name = "progress", type = "flash.events.ProgressEvent")] 
	/**
	* Dispatched while finished loading an image
	* @eventType flash.events.Event.COMPLETE
	*/	 
	[Event(name = "complete", type = "flash.events.Event")] 
	/**
	* Dispatched while select image
	* @eventType flash.events.Event.SELECT
	*/	 
	[Event(name = "select", type = "flash.events.Event")] 
	/**
	 * AbstractImageBrowser
	 * @author Wissarut Pimanmassuriya
	 */
	public class AbstractImageBrowser extends EventDispatcher {
		
		protected var type:/*FileFilter*/Array;
		protected var size:int;
		
		public var content:Loader;
		
		protected var loader:Loader;
		
		/**
		 * 
		 * @param	type		array contains FileFilter object to browse
		 * @param	size		file size in bytes de fault is 0(size doesn't checked);
		 * @param	smoothing		
		 */
		public function AbstractImageBrowser (type:/*FileFilter*/Array, size:int = 0, smoothing:Boolean = false):void {
				this.type = type;
				this.size = size;
		}
	
		public function browse ():void {
		}
		
		protected function dispatchProgressEvent ():void {
				if(loader) dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, loader.contentLoaderInfo.bytesLoaded, loader.contentLoaderInfo.bytesTotal));
		}
		protected function dispatchCompleteEvent ():void {
				dispatchEvent(new Event(Event.COMPLETE, false, false));
		}
		protected function dispatchErrorEvent (errorMessage:String):void {
				dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, errorMessage));
		}
		private var loadPath:String;
		public function loadFile (url:String):void {
				content = null;
				clearLoader();
				loader = new Loader();
				loadPath = url;
				//trace(loadPath);
				loader.load(new URLRequest(url));
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0 ,true);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0 ,true);
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0 ,true);
		}
		
		protected function progressHandler(e:ProgressEvent):void {
			dispatchProgressEvent();
		}
		
		private function completeHandler(e:Event):void {
			content = loader;
			e.target.removeEventListener(Event.COMPLETE, completeHandler);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatchCompleteEvent();
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void {
			e.target.removeEventListener(Event.COMPLETE, completeHandler);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatchErrorEvent("File not found @"+loadPath);
		}
		private function clearLoader ():void {
				if(loader) {
					try {
						loader.close();
					} catch (e:Error) {
					}
					try {
						loader.unload();
					} catch (e:Error) {
					}
				}
				
		}
		
	}
	
}